package puzzle.projecteuler.p200;

import astudy.util.AdvMath;

public class Problem134B {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		long s = System.currentTimeMillis();		
		int max = 1000004;
		Integer[] ps = AdvMath.primes(max);
		int pow10 = 10;
		long sum = 0;
		for (int i = 2; i < ps.length-1; i ++) {
			int p1 = ps[i];
			int p2 = ps[i+1];
			if (p1 > pow10) {
				pow10 *= 10;
			}
			long[] ans = AdvMath.equation(pow10, p2);
			long m = (long)Math.ceil(1.0*ans[0]*p1/p2);
			long t = (long)pow10*(m*p2 - ans[0]*p1) + p1;
			while (t > (long)pow10*p2) t-=(long)pow10*p2;
			while (t < 0) t+=(long)pow10*p2;
			sum += t;
		}
		System.out.println("sum = " + sum);
		System.out.println((System.currentTimeMillis() - s) + " ms");
	}
	
}
